//@author A0103459H

#include "DateTime.h"

using namespace std;

const string DateTime::SLASH = "/";
const string DateTime::SPACE = " ";
const string DateTime::COLON = ":";
const string DateTime::ZERO_STRING = "0";

DateTime::DateTime(void){
	//Default constructors. Sets all attributes to DEFAULT_VALUE.
	_year=DEFAULT_VALUE;
	_month=DEFAULT_VALUE;
	_day=DEFAULT_VALUE;
	_hour=DEFAULT_VALUE;
	_minute=DEFAULT_VALUE;
	_sec=DEFAULT_VALUE;
}


DateTime::~DateTime(void) {
}


DateTime::DateTime(int year, int month, int day) {
	_year = year;
	_month = month;
	_day = day;
	_hour = DEFAULT_VALUE;
	_minute = DEFAULT_VALUE;
	_sec = DEFAULT_VALUE;
}

DateTime::DateTime(int year, int month, int day, int hour, int minute) {
	_year = year;
	_month = month;
	_day = day;
	_hour = hour;
	_minute = minute;
	_sec = DEFAULT_VALUE;
}

DateTime::DateTime(int year,int month,int day,int hour,int minute,int second) {
	_year = year;
	_month = month;
	_day = day;
	_hour = hour;
	_minute = minute;
	_sec = second;
}

int DateTime::getYear(){ 
	return _year;
}

void DateTime::setYear(int year){
	_year=year;
}

int DateTime::getMonth(){ 
	return _month;
}

void DateTime::setMonth(int month){
	_month=month;
}

int DateTime::getDay(){
	return _day;
}

void DateTime::setDay(int day){
	_day=day;
}

int DateTime::getHour(){ 
	return _hour;
}

void DateTime::setHour(int hour){
	_hour=hour;
}

int DateTime::getMinute(){
	return _minute;
}

void DateTime::setMinute(int minute){
	_minute=minute;
}

int DateTime::getSec(){
	return _sec;
}

void DateTime::setSec(int second){
	_sec=second;
}

void DateTime::setDateTime(int year, int month, int day) {
	_year = year;
	_month = month;
	_day = day;
	_hour = DEFAULT_VALUE;
	_minute = DEFAULT_VALUE;
	_sec = DEFAULT_VALUE;
}

void DateTime::setDateTime(int year, int month, int day, int hour, int minute) {
	_year = year;
	_month = month;
	_day = day;
	_hour = hour;
	_minute = minute;
	_sec = DEFAULT_VALUE;
}

void DateTime::setDateTime(int year, int month, int day, int hour, int minute, int second) {
	_year = year;
	_month = month;
	_day = day;
	_hour = hour;
	_minute = minute;
	_sec = second;
}

string DateTime::getDateTimeString(){
	string monthNames [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	string dayName = getDayNameString();
	string monthName = monthNames[getMonth()-1];
	string dayString = addZeroFromFront(to_string(_day));
	string hourString = addZeroFromFront(to_string(_hour));
	string minute_String = addZeroFromFront(to_string(_minute));
	string secString = addZeroFromFront(to_string(_sec));
	string yearString = addZeroFromFront(to_string(_year));

	string output = dayName + SPACE + monthName + SPACE + dayString + SPACE + hourString + SPACE + minute_String + SPACE + secString + SPACE + yearString;
	return output;
}

string DateTime::getDayNameString() {
	struct tm time;

	string dayName[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
	time.tm_year=_year-1900;
	time.tm_mon=_month-1;
	time.tm_mday=_day;
	time.tm_hour = 0;
	time.tm_min = 0;
	time.tm_sec = 0;
	mktime(&time);
	
	return dayName[time.tm_wday];
}

string DateTime::addZeroFromFront(string inString){
	if (inString.size()==1){
		return ZERO_STRING+inString;
	}else if (inString.size()==0){
		return ZERO_STRING+ZERO_STRING;
	}else{
		return inString;
	}
}

int DateTime::compareTo(DateTime another){
	if (this->getYear() != another.getYear()){
		return this->getYear() - another.getYear();
	}else if(this->getMonth() != another.getMonth()){
		return this->getMonth() - another.getMonth();
	}else if(this->getDay() != another.getDay()){
		return this->getDay() - another.getDay();
	}else if(this->getHour() != another.getHour()){
		return this->getHour() - another.getHour();
	}else if(this->getMinute() != another.getMinute()){
		return this->getMinute() - another.getMinute();
	}else if(this->getSec() != another.getSec()){
		return this->getSec() - another.getSec();
	}else{
		return ZERO;
	}
}